package com.tcu.boiler.factory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tcu.boiler.factory.entity.Boiler;
import com.tcu.boiler.factory.entity.Vo.BoilerCondition;
import com.tcu.boiler.factory.service.IBoilerService;
import com.tcu.exception.SmartException;
import com.tcu.result.R;
import org.mockito.internal.matchers.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/boiler")
public class BoilerController {
    private IBoilerService boilerService;

    /**
     * 需要有的功能有，
     * 增删改查
     * 查询，条件分页查询。
     *
     */
    @PostMapping("add")
    public R add(@RequestBody Boiler boiler){
        if (boiler.getId()!=null) {
            throw new SmartException(20001,"id不为空");
        }
        boolean b = boilerService.save(boiler);
        return b ? R.success() : R.error();
    }
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = boilerService.removeById(id);
        return b ? R.success() : R.error();
    }
    @PostMapping("update")
    public R update(@RequestBody Boiler boiler){
        if (boiler.getId()==null) {
            throw new SmartException(20001,"id为空");
        }
        boolean b = boilerService.updateById(boiler);
        return b ? R.success() : R.error();
    }

    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        Boiler byId = boilerService.getById(id);
        return R.success().data("boiler", byId);
    }

    @PostMapping("listCondition/{page}/{size}")
    public R listCondition(@PathVariable long page, @PathVariable long size, @RequestBody BoilerCondition condition){
        Page<Boiler> p = new Page<>(page,size);
        QueryWrapper<Boiler> wrapper = new QueryWrapper<>();
        NotNull notNull = NotNull.NOT_NULL;
        wrapper.eq(notNull.matches(condition.getIsDangerous()),"is_dangerous",condition.getIsDangerous())
                .like(notNull.matches(condition.getTypeLike()),"type",condition.getTypeLike())
                .like(notNull.matches(condition.getLocationLike()),"location",condition.getLocationLike())
                .like(notNull.matches(condition.getBoilerProducerNameLike()),"boiler_producer_name",condition.getBoilerProducerNameLike() );
        boilerService.page(p,wrapper);
        return R.success().data("pageInfo",p);
    }

    @GetMapping("getBoilersByMarkId/{markId}")
    public R getBoilersByMarkId(@PathVariable String markId){
        QueryWrapper<Boiler> wr = new QueryWrapper<>();

        wr.eq("mark_id",markId);
        List<Boiler> list = boilerService.list(wr);
        return R.success().data("list", list);
    }

    @Autowired
    public void setBoilerService(IBoilerService boilerService) {
        this.boilerService = boilerService;
    }
}
